package com.topc.participant.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.topc.base.exception.Asserts;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.base.utils.PageUtils;
import com.topc.participant.mapper.CourseUserMidMapper;
import com.topc.participant.mapper.UserInfoMapper;
import com.topc.participant.model.po.CourseInfo;
import com.topc.participant.mapper.CourseInfoMapper;
import com.topc.participant.model.po.CourseUserMid;
import com.topc.participant.model.po.UserInfo;
import com.topc.participant.model.vo.CourseShowVo;
import com.topc.participant.model.vo.PeopleShowVo;
import com.topc.participant.service.ICourseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topc.participant.service.ICourseUserMidService;
import com.topc.participant.service.IUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程信息 服务实现类
 * </p>
 *
 * @author Gogoing
 */
@Slf4j
@Service
public class CourseInfoServiceImpl extends ServiceImpl<CourseInfoMapper, CourseInfo> implements ICourseInfoService {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ICourseUserMidService courseUserMidService;

    @Autowired
    private CourseUserMidMapper courseUserMidMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void updateInfoById(Long id, int status) {
        CourseInfo courseInfo = new CourseInfo();
        courseInfo.setId(id);
        courseInfo.setStatus(status);
        boolean update = this.updateById(courseInfo);
        if (!update) Asserts.fail("更新失败");
    }

    @Override
    public PageResult<CourseShowVo> searchByPage(PageParams pageParams, int status,Long groupId,String courseName) {
        Page<CourseShowVo> courseShowVoPage =this.getBaseMapper().searchPage(pageParams.page(),status,groupId,courseName);
        return PageUtils.pageToResult(courseShowVoPage);
    }

    @Override
    public List<PeopleShowVo> getListAllPeople(Long id) {
        //通过id找课程信息
        CourseInfo courseInfo = this.getById(id);
        if(ObjectUtil.isEmpty(courseInfo)) Asserts.fail("课程不存在");
        //通过课程信息中的老师编号找老师信息
        UserInfo teacher = userInfoMapper.getUserInfoByNumber(courseInfo.getTutorNumber());
        if (ObjectUtil.isEmpty(teacher)) Asserts.fail("老师不存在");
        //根据中间表的课程id寻找对应的学生账号，再从用户信息中找到想要的属性
        List<PeopleShowVo> list = courseUserMidMapper.getStudentById(id);
        //在list内依次存入身份为学生
        list.forEach(peopleShowVo -> peopleShowVo.setIdentify("学生"));
        //在集合内加入教师信息
        PeopleShowVo peopleShowVo = new PeopleShowVo();
        peopleShowVo.setIdentify("导师");
        peopleShowVo.setUsername(teacher.getUsername());
        peopleShowVo.setUserNumber(teacher.getNumber());
        list.add(0,peopleShowVo);
        return list;
    }

    @Override
    public void saveCourseInfo(CourseInfo courseInfo) {
        boolean save = this.save(courseInfo);
        if (!save){
            Asserts.fail("课程添加失败");
        }
    }

    @Override
    public void deleteCourseInfo(Long id) {
        boolean remove = this.removeById(id);
        if (!remove){
            Asserts.fail("课程删除失败");
        }
    }

    @Override
    public void updateCourseInfo(CourseInfo courseInfo) {
        boolean update = this.updateById(courseInfo);
        if (!update){
            Asserts.fail("课程修改失败");
        }
    }
}
